/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using EXtremecode.BusinessLayer;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Web.Script.Services;
using EXtremecode.DataAccessLayer;
using System.Data;

/// <summary>
/// Summary description for AWWebService
/// </summary>
/// 
namespace EXtremecode.WebServices
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]

    public class AWWebService : System.Web.Services.WebService
    {

        public AWWebService()
        {

            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod(EnableSession = true)]
        public bool IsDuplicate(string tableName,string whereClause)
        {
            DataTable dt=  DataAccessor.ExecuteFillQuery(
                string.Format("select * from {0} where {1}", tableName, whereClause), false);
            string dataTableWhareClaues = string.Empty;
            if (dt.Columns.Contains("IsActive"))
            {
                dataTableWhareClaues = "IsActive = 1";
            }
            return (dt.Select(dataTableWhareClaues).Length   > 0);
        }

        [WebMethod(EnableSession = true)]
        public string[] GetFieldValues(string entityName, string fieldNames, string key, string keyValue)
        {
            return GetFieldValues_WithFilter(entityName, fieldNames, key, keyValue, string.Empty);
        }
        [WebMethod(EnableSession = true)]
        public string[] GetFieldValues_WithFilter(string entityName, string fieldNames, string key, string keyValue,string whereClause)
        {
            List<string> list = new List<string>();
            BusinessObjectCollection boCol = BusinessBase.GetBOCollection(entityName) as BusinessObjectCollection;
            if (boCol == null)
            {
                return null;
            }

            BusinessObjectPaper boPaper = null;
            lock (boCol)
            {
                //key may contains more then one column
                string currentWhereCluase = boCol.WhereClause;
                try
                {
                    if (!string.IsNullOrEmpty(whereClause))
                    {
                        boCol.WhereClause += (!string.IsNullOrEmpty(boCol.WhereClause)) ?
                            " and " : string.Empty
                            + whereClause;

                    }
                    string[] keyFields = key.Split(',');
                    string[] keyFieldValues = keyValue.Split(',');
                    for (int i = 0; i < keyFields.Length; i++)
                    {
                        string keyField = keyFields[i];
                        string keyFieldValue = keyFieldValues[i];
                        boCol[keyField] = keyFieldValue;
                    }
                    boPaper = boCol.GetBOPaper();
                }
                finally
                {
                    boCol.WhereClause = currentWhereCluase;
                }
            }
            if (boPaper == null)
            {
                return null;
            }

            //get Values
            string[] strsFieldNames = fieldNames.Split(',');
            for (int i = 0; i < strsFieldNames.Length; i++)
            {
                list.Add((boPaper[strsFieldNames[i]] != null) ?
                    boPaper[strsFieldNames[i]].ToString() :
                    string.Empty);

            }
            return list.ToArray();

        }


        [WebMethod(EnableSession = true)]
        public string[][] GetListOfFieldValues_WithFilter(string entityName, string fieldNames
            , string referencedKey, string referencedKeyValue)
        {
            return GetListOfFieldValues_WithFilter(entityName, fieldNames, referencedKey, referencedKeyValue, string.Empty, false);
        }

        [WebMethod(EnableSession = true)]
        public string[][] GetListOfFieldValues_WithFilter(string entityName, string fieldNames
            , string referencedKey, string referencedKeyValue
            , string whereClause)
        {
            return GetListOfFieldValues_WithFilter(entityName, fieldNames, referencedKey, referencedKeyValue, whereClause, false);
        }
        private string[][] GetListOfFieldValues_WithFilter(string entityName, string fieldNames
            , string referencedKey, string referencedKeyValue
            , string whereClause,bool returnFirstRow)
        {
            long recordsCount = 0;
            List<string[]> lstListOfValues = new List<string[]>();
            BusinessObjectCollection boCol = BusinessBase.GetBOCollection(entityName) as BusinessObjectCollection;
            if (boCol == null)
            {
                return null;
            }

            lock (boCol)
            {
                
                //save current where clause.
                string currentWhereCluase = boCol.WhereClause;
                try
                {
                    if (!string.IsNullOrEmpty(whereClause))
                    {
                        boCol.WhereClause += (!string.IsNullOrEmpty(boCol.WhereClause)) ?
                            " and " : string.Empty
                            + whereClause;

                    }
                     
                    //key may contains more then one column
                    string[] keyFields = referencedKey.Split(',');
                    string[] keyFieldValues = referencedKeyValue.Split(',');
                    for (int i = 0; i < keyFields.Length; i++)
                    {
                        string keyField = keyFields[i];
                        string keyFieldValue = keyFieldValues[i];
                        boCol[keyField] = keyFieldValue;
                    }
                    
                        foreach (BusinessObjectPaper boPaper in boCol.GetBOPapers())
                        {
                            List<string> lstValues = new List<string>();
                            string[] strsFieldNames = fieldNames.Split(',');
                            for (int i = 0; i < strsFieldNames.Length; i++)
                            {
                                lstValues.Add((boPaper[strsFieldNames[i]] != null) ?
                                    boPaper[strsFieldNames[i]].ToString() :
                                    string.Empty);

                            }

                            lstListOfValues.Add(lstValues.ToArray());

                            if (returnFirstRow)
                            {
                                break;
                            }
                        }
                        
                    
                }
                finally
                {
                    boCol.WhereClause = currentWhereCluase;
                    boCol.Close();
                }

               


            }

            return lstListOfValues.ToArray();
           
        }

    }

}